// pages/settings/settings.js
var util = require("../../utils/util.js")

Page({
  data: {
    uid: '',
    headUrl: "",
    username: '',
    sex: '',
    checked: '',
    array: [{ sex: '男', checked: '' }, { sex: '女' }],
    display: 'none',
    display1: 'none',
    imgUrl:""
  },

  onLoad: function () {
    var that = this;
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        // success
        that.setData({
          uid: res.data.uid,
          imgUrl: res.data.headImg,
          username: res.data.nickname,
          sex: res.data.sex
        })
      },
    })
  },
  settings: function () {
    wx.navigateTo({
      url: '../address/address',
    })
  },
  listenerRadioGroup: function (e) {
    var that = this;
    that.setData({
      sex: e.detail.value,
      display: 'none'
    })
    console.log(e.detail.value)
  },
  // 点击性别出弹窗
  sex: function () {
    var that = this;
    that.setData({
      display: 'block'
    })
  },
  // 点击出现弹窗
  name: function () {
    var that = this;
    that.setData({
      display1: 'block'
    })
  },
  NaneInput: function (e) {
    var that = this;
    that.setData({
      username: e.detail.value
    })
  },
  // 点击关闭姓名输入框
  NameClick: function () {
    var that = this;
    that.setData({
      display1: 'none'
    })
  },
  submit: function () {
    console.log(this.data.imgUrl)
    var aaa = wx.getStorageSync("userInfo");
    var that = this

    wx.request({
      url: "http://1.aiym.applinzi.com/aiyouwai.php",
      data: {
        type: "changeInfo",
        paras: {
          uid: this.data.uid,
          imgUrl: this.data.imgUrl,
          nickname: this.data.username,
          sex: this.data.sex
        },
      },
      header: { "content-type": "application/json" },
      success: function (res) {

        aaa.nickname = that.data.username
        aaa.headImg = that.data.imgUrl
        aaa.sex = that.data.sex
        wx.setStorage({
          key: 'userInfo',
          data: aaa
        })
        //设置弹窗让用户点击成功修改
        wx.showModal({
          content: '修改成功',
          showCancel: false,
          success: function (res) {
            wx.navigateBack({
              delta: 1, // 回退前 delta(默认为1) 页面
            })
          }
        });
      },
    })
  },
  change: function () {
    util.chooseImg(this)
  }
})